package com.example.its2021606134chh.TrafficQueryModule;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.example.its2021606134chh.TLModule.TLightsRViewAdapter;
import com.example.its2021606134chh.ThresholdModule.ThresholdParams;
import com.google.gson.Gson;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TQFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TQFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private TextView tvTQPM25;
    private TextView tvTQTem;
    private TextView tvTQHum;
    private TextView tvTQRoad1;
    private TextView tvTQRoad1Color;
    private TextView tvTQRoad2;
    private TextView tvTQRoad2Color;
    private TextView tvTQRoad3;
    private TextView tvTQRoad3Color;
    private int[] roadList = {1, 2, 3};
    private Timer timer;
    private TimerTask timerTask;

    public TQFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TQFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TQFragment newInstance(String param1, String param2) {
        TQFragment fragment = new TQFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_t_q, container, false);
        // Inflate the layout for this fragment
        tvTQPM25 = (TextView) view.findViewById(R.id.tvTQPM25);
        tvTQTem = (TextView) view.findViewById(R.id.tvTQTem);
        tvTQHum = (TextView) view.findViewById(R.id.tvTQHum);
        tvTQRoad1 = (TextView) view.findViewById(R.id.tvTQRoad1);
        tvTQRoad1Color = (TextView) view.findViewById(R.id.tvTQRoad1Color);
        tvTQRoad2 = (TextView) view.findViewById(R.id.tvTQRoad2);
        tvTQRoad2Color = (TextView) view.findViewById(R.id.tvTQRoad2Color);
        tvTQRoad3 = (TextView) view.findViewById(R.id.tvTQRoad3);
        tvTQRoad3Color = (TextView) view.findViewById(R.id.tvTQRoad3Color);
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                    @Override
                    public void onFailure(Exception e) {

                    }

                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        ThresholdParams params = gson.fromJson(json, ThresholdParams.class);
                        int Temperature = Integer.parseInt(params.getTemperature());
                        int Humidity = Integer.parseInt(params.getHumidity());
                        int Pm25 = Integer.parseInt(params.getPm25());
                        tvTQPM25.setText("PM2.5：" + Pm25);
                        tvTQTem.setText("湿   度：" + Temperature);
                        tvTQHum.setText("湿   度：" + Humidity);
                    }
                }, "get_all_sense", new String[]{"UserName"}, "user1");
                for (int road : roadList) {
                    HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                        @Override
                        public void onFailure(Exception e) {

                        }

                        @Override
                        public void onResponse(String json) {
                            Gson gson = new Gson();
                            RoadStatus status = gson.fromJson(json, RoadStatus.class);
                            int roadStatus = Integer.parseInt(status.getStatus());
                            switch (road) {
                                case 1:
                                    switch (roadStatus) {
                                        case 1:
                                            tvTQRoad1.setText("1号道路：通畅");
                                            tvTQRoad1Color.setBackgroundColor(Color.parseColor("#0ebd12"));
                                            break;
                                        case 2:
                                            tvTQRoad1.setText("1号道路：较通畅");
                                            tvTQRoad1Color.setBackgroundColor(Color.parseColor("#98ed1f"));
                                            break;
                                        case 3:
                                            tvTQRoad1.setText("1号道路：拥挤");
                                            tvTQRoad1Color.setBackgroundColor(Color.parseColor("#ffff01"));
                                            break;
                                        case 4:
                                            tvTQRoad1.setText("1号道路：堵塞");
                                            tvTQRoad1Color.setBackgroundColor(Color.parseColor("#ff0103"));
                                            break;
                                        case 5:
                                            tvTQRoad1.setText("1号道路：爆表");
                                            tvTQRoad1Color.setBackgroundColor(Color.parseColor("#4c060e"));
                                            break;
                                        default:
                                            break;
                                    }
                                    break;
                                case 2:
                                    switch (roadStatus) {
                                        case 1:
                                            tvTQRoad2.setText("2号道路：通畅");
                                            tvTQRoad2Color.setBackgroundColor(Color.parseColor("#0ebd12"));
                                            break;
                                        case 2:
                                            tvTQRoad2.setText("2号道路：较通畅");
                                            tvTQRoad2Color.setBackgroundColor(Color.parseColor("#98ed1f"));
                                            break;
                                        case 3:
                                            tvTQRoad2.setText("2号道路：拥挤");
                                            tvTQRoad2Color.setBackgroundColor(Color.parseColor("#ffff01"));
                                            break;
                                        case 4:
                                            tvTQRoad2.setText("2号道路：堵塞");
                                            tvTQRoad2Color.setBackgroundColor(Color.parseColor("#ff0103"));
                                            break;
                                        case 5:
                                            tvTQRoad2.setText("2号道路：爆表");
                                            tvTQRoad2Color.setBackgroundColor(Color.parseColor("#4c060e"));
                                            break;
                                        default:
                                            break;
                                    }
                                    break;
                                case 3:
                                    switch (roadStatus) {
                                        case 1:
                                            tvTQRoad3.setText("3号道路：通畅");
                                            tvTQRoad3Color.setBackgroundColor(Color.parseColor("#0ebd12"));
                                            break;
                                        case 2:
                                            tvTQRoad3.setText("3号道路：较通畅");
                                            tvTQRoad3Color.setBackgroundColor(Color.parseColor("#98ed1f"));
                                            break;
                                        case 3:
                                            tvTQRoad3.setText("3号道路：拥挤");
                                            tvTQRoad3Color.setBackgroundColor(Color.parseColor("#ffff01"));
                                            break;
                                        case 4:
                                            tvTQRoad3.setText("3号道路：堵塞");
                                            tvTQRoad3Color.setBackgroundColor(Color.parseColor("#ff0103"));
                                            break;
                                        case 5:
                                            tvTQRoad3.setText("3号道路：爆表");
                                            tvTQRoad3Color.setBackgroundColor(Color.parseColor("#4c060e"));
                                            break;
                                        default:
                                            break;
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                    }, "get_road_status", new String[]{"RoadId", "UserName"}, road,"user1");
                }
            }
        };
        timer.schedule(timerTask, 0, 3000);
        return view;
    }

}